package b6.vacancyshare;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

public class Favorit extends SQLiteOpenHelper {
    /** Called when the activity is first created. */
	
	private static final String DATABASE_NAME="vsharelite.db";
	private static final int SCHEMA_VERSION=1;
	public Favorit(Context context) {
    	super(context,DATABASE_NAME,null,SCHEMA_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
    	String sql = "CREATE TABLE favorit (idfavorit TEXT NOT NULL, idlowongan TEXT NOT NULL, PRIMARY KEY(idfavorit, idlowongan);";
    	db.execSQL(sql);
    }
    
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    	
    }
    
    public Cursor getAll() {
    	return (getReadableDatabase().rawQuery("SELECT idfavorit, idlowongan FROM favorit", null));
    }
    
    public Cursor getById(String id) {
    	String[] args = {id};
    	return (getReadableDatabase().rawQuery("SELECT idfavorit, idlowongan FROM favorit WHERE idfavorit=?", args));
    }
    
    public void insert(String id, String idlowongan) {
    	ContentValues cv = new ContentValues();
    	cv.put("idfavorit", id);
    	cv.put("idlowongan", idlowongan);
    	getWritableDatabase().insert("favorit","idfavorit",cv);
    	
    }
    
    public String getIdFavorit(Cursor c) {
    	return (c.getString(0));
    }
    public String getIdLowongan(Cursor c) {
    	return (c.getString(1));
    }
    
    public void delete(String id, String idlowongan) {
    	String[] args = {id,idlowongan};
    	getWritableDatabase().delete("favorit", "id=? AND idlowongan=?", args);
    }
}